import { CommentHeader } from "@/model/common-header.model";
import { CurrentMusic } from "@/model/current-music.model";
import { CommentType, CommonIdType, PlayModel } from "@/model/id-or-enum.model";
import { MusicInfo } from "@/model/music-info.model";
import { CurrentTimeInfo, FullTimeInfo } from "@/model/playing-info.model";
import { MutationTree } from "vuex";
import { AppState } from "./types";
import local from "@/utils/local-utils";
import _ from "lodash";
import { LoginResponse } from "@/model/response-type.model";

export const mutations: MutationTree<AppState> = {
  /**
   * 设置用户信息
   */
   setUserInfo(state: AppState, value: Partial<LoginResponse>) {
    state.userInfo = value;
    local.set("userInfo", value);
  },
  /**
   * 设置登录后跳转到哪个页面
   */
  setPrevPath(state: AppState, value: string) {
    state.prevPath = value;
    local.set("prevPath", value);
  },
  /**
   * 设置登录框的显示状态
   */
  setLoginDialogVisible(state: AppState, value: boolean) {
    state.loginDialogVisible = value;
    local.set("loginDialogVisible", value);
  },
  /**
   * 清空播放列表
   */
  clearMusicList(state: AppState) {
    state.musicUrlList = [];
    local.set("musicUrlList", []);
  },
  /**
   * 向播放列表中添加
   */
  addToMusicList(state: AppState, music: MusicInfo, index?: number) {
    if (state.musicUrlList.find(x => x.id === music.id)) return;
    if (index != null) {
      state.musicUrlList.splice(index + 1, 0, music);
    } else {
      state.musicUrlList.push(music);
    }
    local.set("musicUrlList", state.musicUrlList);
  },
  /**
   * 从播放列表中删除
   */
  deleteFromMusicList(state: AppState, id: CommonIdType) {
    _.remove(state.musicUrlList, x => x.id === id);
  },
  /**
   * 设置当前播放的歌曲信息
   */
  setCurrentMusicInfo(state: AppState, music: CurrentMusic) {
    state.nowMusic = music;
    local.set("nowMusic", music);
  },
  /**
   * 清空当前播放信息
   */
  clearCurrentMusicInfo(state: AppState) {
    state.nowMusic = {
      index: -1,
      id: '',
      nowImgUrl: '',
      nowMusicUrl: '',
      nowName: '',
      nowArtists: []
    }
  },
  /**
   * 设置当前歌曲的总时长信息
   */
  setFullTimeInfo(state: AppState, fullTime: FullTimeInfo) {
    state.full = fullTime;
    local.set("full", fullTime);
  },
  /**
   * 设置当前歌曲的已播放时长信息
   */
  setCurrentTimeInfo(state: AppState, currentTime: CurrentTimeInfo) {
    state.current = currentTime;
    local.set("current", currentTime);
  },
  /**
   * 设置歌曲的播放/暂停
   */
  setIsPlaying(state: AppState, value: boolean) {
    state.isPlaying = value;
    local.set("isPlaying", value);
  },
  /**
   * 设置播放工具栏的显示/隐藏
   */
  setShowController(state: AppState, value: boolean) {
    state.isController = value;
    local.set("isController", value);
  },
  /**
   * 设置当前mv的id
   */
  setMvId(state: AppState, id: CommonIdType) {
    state.mvId = id;
    local.set("mvId", id);
  },
  /**
   * 设置当前展示的歌曲详情信息
   */
  setMoreMsg(state: AppState, music: MusicInfo) {
    state.moreMsg = music;
    local.set("moreMsg", music);
  },
  /**
   * 设置当前的播放模式（单曲、顺序、随机）
   */
  setPlayModel(state: AppState, mode: PlayModel) {
    state.playModel = mode;
    local.set("playModel", mode);
  },
  /**
   * 设置评论页面的对象类型（评论的是歌曲还是歌单或者专辑）
   */
  setCommentType(state: AppState, type: CommentType) {
    state.commentType = type;
    local.set("commentType", type);
  },
  /**
   * 设置传给评论页面的信息
   */
  setCommentHeader(state: AppState, header: CommentHeader) {
    state.commentHeader = header;
    local.set("commentHeader", header);
  },
};
